import axios from 'axios'
import { signatureFoo } from '@/utils/signature'
axios.defaults.timeout = 15 * 1000
axios.defaults.baseURL = '/api'
axios.defaults.withCredentials = true
axios.defaults.headers['Content-Type'] = 'application/x-www-form-urlencoded'

// http request 拦截器
axios.interceptors.request.use()

// http response 拦截器
axios.interceptors.response.use()

/**
 * 封装通用的请求方法
 * @param {String} url // 请求地址
 * @param {JSON} params // 入参
 * @param {String} method // 方法，默认 get
 */
export function request (url: string, params = {}, method = 'get') {
  const authorization = localStorage.token
  axios.defaults.headers.Authorization = authorization
  const buffer = signatureFoo(params)
  axios.defaults.headers.Signature = buffer.signature
  axios.defaults.headers.Timestamp = buffer.timeStamp
  return new Promise((resolve, reject) => {
    axios[method](url,
      params,
      .then((response) => {
        resolve(response)
      })
      .catch((err) => {
        reject(err)
      })
  })
}

